﻿using MapleLeaf.Application.CookBook.Models;
using MapleLeaf.Core.Dto;
using MapleLeaf.Core.Extension;
using MapleLeaf.Model.CookBook;
using MapleLeaf.Service.CookBook;
using Mapster;
using SqlSugar;
using System.Linq.Expressions;

namespace MapleLeaf.Application.CookBook
{
    /// <summary>
    /// 菜谱管理客户端
    /// </summary>
    public class CookBookManagementClient : IApplicationClient
    {
        private readonly RecipeService _recipeService;
        private readonly IngredientService _ingredientService;

        /// <summary>
        /// 
        /// </summary>
        public CookBookManagementClient(RecipeService recipeService, IngredientService ingredientService)
        {
            _recipeService = recipeService;
            _ingredientService = ingredientService;
        }

        #region 菜谱
        /// <summary>
        /// 获取食材列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<RecipeEntity>> GetRecipePageListAsync(Expression<Func<RecipeEntity, bool>> whereExp, PageModel pagination)
        {
            var res = await _recipeService.GetPageListAsync(whereExp, pagination);
            return res;
        }
        /// <summary>
        /// 获取所有菜谱
        /// </summary>
        /// <returns></returns>
        public async Task<List<RecipeEntity>> GetRecipeListAsync(Expression<Func<RecipeEntity, bool>> whereExp)
        {
            return await _recipeService.GetListAsync(whereExp);
        }
        /// <summary>
        /// 获取菜谱信息
        /// </summary>
        /// <returns></returns>
        public async Task<RecipeEntity> GetRecipeInfoAsync(long id)
        {
            var res = await _recipeService.GetByIdAsync(id);
            return res;
        }
        /// <summary>
        /// 保存菜谱
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> SaveRecipeAsync(RecipeEntity entity)
        {
            if (entity.Id.IsNullOrEmpty())
            {
                return await _recipeService.InsertAsync(entity);
            }
            else
            {
                return await _recipeService.UpdateAsync(entity);
            }
        }
        /// <summary>
        /// 删除菜谱
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> DeleteRecipeAsync(Expression<Func<RecipeEntity, bool>> whereExpression)
        {
            return await _recipeService.DeleteAsync(whereExpression);
        }
        #endregion
        #region 食材
        /// <summary>
        /// 获取食材列表
        /// </summary>
        /// <returns></returns>
        public async Task<PaginationModel<IngredientVo>> GetIngredientPageListAsync(Expression<Func<IngredientEntity, bool>> whereExp, PaginationModel pagination)
        {
            var page = pagination.Adapt<PaginationModel<IngredientVo>>();
            RefAsync<int> total = 0;
            var res = await _ingredientService.GetPageListAsync(whereExp, page.PageIndex, page.PageSize, total);
            page.Total = total;
            page.List = res.Adapt<List<IngredientVo>>();
            return page;
        }
        /// <summary>
        /// 获取所有食材
        /// </summary>
        /// <returns></returns>
        public async Task<List<IngredientEntity>> GetIngredientListAsync(Expression<Func<IngredientEntity, bool>> whereExp)
        {
            return await _ingredientService.GetListAsync(whereExp);
        }
        /// <summary>
        /// 获取菜单管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<IngredientEntity> GetIngredientInfoAsync(long id)
        {
            var res = await _ingredientService.GetByIdAsync(id);
            return res;
        }
        /// <summary>
        /// 保存食材
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> SaveIngredientAsync(IngredientEntity entity)
        {
            if (entity.Id == 0)
            {
                return await _ingredientService.InsertAsync(entity);
            }
            else
            {
                return await _ingredientService.UpdateAsync(entity);
            }
        }
        /// <summary>
        /// 保存食材
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(List<IngredientEntity> entitys)
        {

            return await _ingredientService.InsertAsync(entitys) > 0;

        }
        /// <summary>
        /// 删除食材
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> DeleteIngredientAsync(Expression<Func<IngredientEntity, bool>> whereExpression)
        {
            return await _ingredientService.DeleteAsync(whereExpression);
        }
        #endregion

    }
}
